package com.group2.edu.realtime.common.util;/**
 * @Author: Wxs
 * @Description: TODO
 * @Date: 2024/12/14 10:51
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author: Wxs
 * @Description: TODO
 * @Date: 2024/12/14 10:51
 */
public class RedisUtil {
    private static JedisPool jedisPool;
    //静态代码块。类加载的时候执行一次
    static {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setMaxTotal(100);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setMaxWaitMillis(2000);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPool = new JedisPool(jedisPoolConfig,"hadoop102",6379,10000);
    }
    //获取jedis客户端
    public static Jedis getJedis(){
        System.out.println("~~~~获取jedis客户端~~~");
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }
    //关闭jedis客户端
    public static void closeJedis(Jedis jedis){
        System.out.println("~~~~关闭jedis客户端~~~");
        if (jedis != null){
            jedis.close();
        }
    }
    //从redis中读数据
    public static JSONObject readDim(Jedis jedis, String tableName, String id){
        String key = getKey(tableName, id);
        String dimJsonStr = jedis.get(key);
        if (StringUtils.isNotEmpty(dimJsonStr)){
            JSONObject dimJsonObj = JSON.parseObject(dimJsonStr);
            return dimJsonObj;
        }
        return null;
    }
    //向redis放数据
    public static void writeDim(Jedis jedis,String tableName,String id,JSONObject dimJsonObj){
        String key = getKey(tableName, id);
        jedis.setex(key,24*3600,dimJsonObj.toJSONString());
    }

    //获取异步操作redis的客户端
    public static StatefulRedisConnection<String,String> getRedisAsyncConnection(){
        System.out.println("~~~~获取异步操作redis的客户端~~~");
        RedisClient redisClient = RedisClient.create("redis://hadoop102:6379/0");
        StatefulRedisConnection<String, String> redisAsyncConn = redisClient.connect();
        return redisAsyncConn;
    }

    //关闭异步操作Redis的客户端
    public static void closeRedisAsyncConnection(StatefulRedisConnection<String,String> redisConnection){
        System.out.println("~~~~关闭异步操作Redis的客户端~~~");
        if (redisConnection != null && redisConnection.isOpen()){
            redisConnection.close();
        }
    }

    //以异步的方式从redis读取数据
    public static JSONObject readDimAsync(StatefulRedisConnection<String,String> redisAsyncConn,String tableName,String id){
        String key = getKey(tableName, id);
        RedisAsyncCommands<String, String> asyncCommands = redisAsyncConn.async();
        try {
            String dimJsonStr = asyncCommands.get(key).get();
            if (StringUtils.isNotEmpty(dimJsonStr)){
                JSONObject jsonObject = JSON.parseObject(dimJsonStr);
                return jsonObject;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    //以异步方式向redis写数据
    public static void writeDimAsync(StatefulRedisConnection<String,String> redisAsyncConn,String tableName,String id,JSONObject dimJsonObj){
        String key = getKey(tableName, id);
        RedisAsyncCommands<String, String> asyncCommands = redisAsyncConn.async();
        asyncCommands.setex(key,24*3600,dimJsonObj.toJSONString());
    }


    public static String getKey(String tableName, String id) {
        String key = tableName + ":" + id;
        return key;
    }
    public static void main(String[] args) {
        Jedis jedis = getJedis();
        System.out.println(jedis.ping());
        closeJedis(jedis);
    }
}
